import * as types from '../mutations'

import http from '../../http'
import { Form } from 'element-ui';

const state = {
  vm: '',
  pro_list: [],
  total: 0,
  pro_detail: {
    user: {},
    data: []
  },
  content_list: []
}

const getters = {
  pro_list: state => state.pro_list,
  total: state => state.total,
  pro_detail: state => state.pro_detail,
  content_list: state => state.content_list,
}

const mutations = {
  [types.SET_VM]: (state, payload) => {
    state.vm = payload.vm
  },
  [types.SET_PRODUCT_LIST]: (state, payload) => {
    state.pro_list = payload
  },
  [types.SET_TOTAL]: (state, payload) => {
    state.total = payload
  },
  [types.SET_CONTENT_LIST]: (state, payload) => {
    state.content_list = payload
  },
}

const actions = {
  query_product({
    commit,
    dispatch,
    state
  }, payload) {
    http('/api/product/query_product', payload, function (res) {
      res.data.forEach(function (pro) {
        pro.img = pro.img.split(',').map(function (url) {
          return { url: res.object.domin + url }
        })
      })
      commit('SET_PRODUCT_LIST', res.data)
      commit('SET_TOTAL', res.object.total)
    })
  },
  set_recommend({
    dispatch,
    commit,
    state
  }, payload) {
    http('/api/product/set_recommend', {
      id: payload.id,
      isrecommend: payload.isrecommend
    }, function (res) {
      if (res.code === 0) {
        dispatch('query_product', payload.other)
      }
    })
  },
  set_dele({
    dispatch,
    commit,
    state
  }, payload) {
    http('/api/product/del_product', {
      id: payload.id,
      isdel: payload.isdel
    }, function (res) {
      if (res.code === 0) {
        dispatch('query_product', payload.other)
      }
    })
  },
  upload({
    dispatch,
    commit, state
  }, payload) {
    state.vm.$axios.post("/api/product/create_product", payload.form, {
      headers: { "Content-Type": "multipart/form-data" }
    }).then((res) => {
      if (res.data.code === 0) {
        state.vm.newVisible = false
        state.vm.$message.success(res.data.message)
        dispatch('query_product', state.vm.searchProduct)
      }
    })
  },
  product_detail({
    dispatch,
    commit, state
  }, payload) {
    http('/api/product/product_detail', { id: state.vm.newPorductParams.id }, res => {
      if (res.code === 0) {
        for (var i in state.vm.newPorductParams) {
          if (i === 'img') {
            state.vm.newPorductParams[i] = res.object.product[i].split(',').map(function (url) {
              return { url: res.object.domin + url }
            })
            console.log(state.vm.newPorductParams.img)
          } else {
            state.vm.newPorductParams[i] = res.object.product[i]
          }
        }
        state.vm.newVisible = true
        state.vm.sfile = []
      }
    })
  },
  modify_product({
    state,
    commit,
    dispatch
  }, payload) {
    var form = new FormData()
    var params = Object.assign({}, state.vm.newPorductParams)
    state.vm.sfile.forEach(function (file, index) {
      form.append('file' + index, file)
    })
    params.img = params.img.map(function (img) {
      return img.url.split('.com/')[1]
    })
    for (var i in params) {
      form.append(i, params[i])
    }
    state.vm.$axios.post("/api/product/modify_product", form, {
      headers: { "Content-Type": "multipart/form-data" }
    }).then((res) => {
        state.vm.$message.success(res.data.message)
        state.vm.newVisible = false
        state.vm.sfile = []
        dispatch('query_product', state.vm.searchProduct)
    })
  },
  query_content({dispatch, commit, state}) {
    http('/api/config/query_content', {}, res => {
      commit('SET_CONTENT_LIST', res.data)
    })
  }
}

export default {
  state,
  getters,
  mutations,
  actions

}
